package com.kky.pp.activity.tab

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.kky.pp.R
import com.kky.pp.databinding.ViewBottomTabBinding
import com.n_add.android.model.BottomTabModel
import com.n_add.android.model.BottomTabViewModel

class BottomNavigationView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
    //private var view: View? = null
//    private var viewHome: ConstraintLayout? = null
//    private var ivHome: ImageView? = null
//    private var tvHomeNum: TextView? = null
//
//    private var viewWlm: ConstraintLayout? = null
//    private var ivWlm: ImageView? = null
//    private var tvWlmNum: TextView? = null
//
//    private var viewChat: ConstraintLayout? = null
//    private var ivChat: ImageView? = null
//    private var tvChatNum: TextView? = null
//
//    private var viewMy: ConstraintLayout? = null
//    private var ivMy: ImageView? = null
//    private var tvMyNum: TextView? = null

    private var lastPosition = -1
    private var bottomTabViewModelList = mutableListOf<BottomTabViewModel>()
    private var bottomTabModellList = mutableListOf<BottomTabModel>()
    private val fragments by lazy { TabHelper.getFragmentList() }
    private var onBottomTabClickListener: OnBottomTabClickListener? = null

    private var binding: ViewBottomTabBinding? = null

    init {
        binding = ViewBottomTabBinding.inflate(LayoutInflater.from(context), this, true)
        //view = LayoutInflater.from(context).inflate(R.layout.view_bottom_tab, this)
//        viewHome = view?.findViewById(R.id.viewHome)
//        ivHome = view?.findViewById(R.id.ivHome)
//        tvHomeNum = view?.findViewById(R.id.tvHomeNum)
        bottomTabViewModelList.add(
            BottomTabViewModel(
                view = binding?.viewHome,
                ivImg = binding?.ivHome,
                tvNum = binding?.tvHomeNum
            )
        )

//        viewWlm = view?.findViewById(R.id.viewWlm)
//        ivWlm = view?.findViewById(R.id.ivWlm)
//        tvWlmNum = view?.findViewById(R.id.tvWlmNum)
        bottomTabViewModelList.add(
            BottomTabViewModel(
                view = binding?.viewWlm,
                ivImg = binding?.ivWlm,
                tvNum = binding?.tvWlmNum
            )
        )

//        viewChat = view?.findViewById(R.id.viewChat)
//        ivChat = view?.findViewById(R.id.ivChat)
//        tvChatNum = view?.findViewById(R.id.tvChatNum)
        bottomTabViewModelList.add(
            BottomTabViewModel(
                view = binding?.viewChat,
                ivImg = binding?.ivChat,
                tvNum = binding?.tvChatNum
            )
        )

//        viewMy = view?.findViewById(R.id.viewMy)
//        ivMy = view?.findViewById(R.id.ivMy)
//        tvMyNum = view?.findViewById(R.id.tvMyNum)
        bottomTabViewModelList.add(
            BottomTabViewModel(
                view = binding?.viewMy,
                ivImg = binding?.ivMy,
                tvNum = binding?.tvMyNum
            )
        )
        initView()
    }

    private fun initView() {
        initListener()
    }

    private fun initListener() {
        bottomTabViewModelList.stream().forEach {
            it.view?.setOnClickListener {

            }
        }
        for (position in 0 until bottomTabViewModelList.size) {
            val bottomTabViewModel = bottomTabViewModelList[position]
            bottomTabViewModel.view?.setOnClickListener {
                if (lastPosition == position)
                    return@setOnClickListener
                upDataUi(position)
                bottomTabClickListener.onTabClick(position, bottomTabViewModel)
            }
        }
    }

    fun init() {
        for (position in 0 until bottomTabViewModelList.size) {
            val bottomTabViewModel = bottomTabViewModelList[position]
            val bottomTabModel = bottomTabModellList[position]
            bottomTabViewModel.ivImg?.setImageResource(
                if (bottomTabModel.selected) {
                    bottomTabModel.selectedIcon
                } else {
                    bottomTabModel.selectedUnIcon
                }
            )
            if (bottomTabModel.selected) {
                bottomTabClickListener.onTabClick(position, bottomTabViewModel)
            }
        }
    }

    fun upDataTagNum(position: Int, num: String) {
        val lastBottomTabViewModel = bottomTabViewModelList[position]
        lastBottomTabViewModel.tvNum?.isVisible = !num.equals("0")
        lastBottomTabViewModel.tvNum?.text = num
    }

    private fun upDataUi(position: Int) {
        val lastBottomTabViewModel = bottomTabViewModelList[lastPosition]
        val lastBottomTabModel = bottomTabModellList[lastPosition]
        lastBottomTabModel.selected = false
        if (lastBottomTabViewModel.ivImg?.tag == null) {
            lastBottomTabViewModel.ivImg?.setImageResource(lastBottomTabModel.selectedUnIcon)
        }

        val nowBottomTabViewModel = bottomTabViewModelList[position]
        val nowBottomTabModel = bottomTabModellList[position]
        nowBottomTabModel.selected = true
        if (nowBottomTabViewModel.ivImg?.tag == null) {
            nowBottomTabViewModel.ivImg?.setImageResource(nowBottomTabModel.selectedIcon)
        }
    }

    fun setBottomTabModelList(bottomTabModellList: MutableList<BottomTabModel>): BottomNavigationView {
        this.bottomTabModellList = bottomTabModellList
        return this
    }

    fun setOnBottomTabClickListener(onBottomTabClickListener: OnBottomTabClickListener): BottomNavigationView {
        this.onBottomTabClickListener = onBottomTabClickListener
        return this
    }

    private val bottomTabClickListener = object : OnBottomTabClickListener {
        override fun onTabClick(tabIndex: Int, bottomTabViewModel: BottomTabViewModel?) {
            onBottomTabClickListener?.onTabClick(tabIndex, bottomTabViewModel)
            fragments.get(tabIndex)
                ?.let { switchFragment(context as FragmentActivity, it, tabIndex) }
        }

    }

    interface OnBottomTabClickListener {
        fun onTabClick(tabIndex: Int, bottomTabViewModel: BottomTabViewModel?)
    }

    private var currentFragment: Fragment? = null
    fun switchFragment(activity: FragmentActivity, index: Int) {
        try {
            switchFragment(activity, fragments.get(index), index)
            upDataUi(index)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun toggle(
        container: Int,
        showFragment: Fragment,
        homeActivity: FragmentActivity,
        data: Any? = null
    ): Fragment {
        val tag = showFragment::class.java.simpleName
        val transaction = homeActivity.supportFragmentManager.beginTransaction()
        if (!showFragment.isAdded) {
            if (currentFragment != null) {
                transaction.hide(currentFragment!!)
                    .add(container, showFragment, tag)
                    .commitNow()
            } else {
                transaction.add(container, showFragment, tag)
                    .commitNow()
            }
        } else {
            currentFragment?.let {
                if (data != null && data.toString() == "like") {
                    transaction.hide(it).show(showFragment).commitAllowingStateLoss()
                } else {
                    transaction.hide(it).show(showFragment).commit()
                }
            }


        }
        currentFragment = showFragment
        return showFragment
    }

    private fun switchFragment(activiyt: FragmentActivity, targetFragment: Fragment, index: Int) {
        if (lastPosition == index) return
        val fragmentManager = activiyt.supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        if (!targetFragment.isAdded) {
            if (currentFragment != null) {
                transaction.hide(currentFragment!!)
                    .add(R.id.home_content, targetFragment, index.toString() + "")
                    .commitAllowingStateLoss()
            } else {
                transaction.add(R.id.home_content, targetFragment, index.toString() + "")
                    .commitAllowingStateLoss()
            }
        } else {
            currentFragment?.let {
                transaction.hide(it).show(targetFragment).commitAllowingStateLoss()
            }
        }
        currentFragment = targetFragment
        lastPosition = index
    }

    fun getFragment(position: Int): Fragment {
        return fragments.get(position) ?: TabHelper.getFragmentList()[position]
    }

    //设置背景
    fun setBg(index: Int) {
        binding?.ivMy?.setBackgroundResource(if (index == 3) R.drawable.shape_me_select_bg else R.drawable.shape_me_unselect_bg)
    }

    fun setImg(imageUrl: String) {
        binding?.ivMy?.let {
            Glide.with(context)
                .load(imageUrl)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .into(it)
            it.tag = "1"
        }
    }
}